<template>
	<view class="content">
		<u--form
			class="item-text-font"
			labelPosition="left"
			label-width="110"
			:model="form"
			ref="checkInForm"
			labelAlign="center"
		>
			<u-form-item label="昵称" prop="nickName">
				<u-input  v-model="form.nickName" maxlength="10"></u-input>
			</u-form-item>
			<u-button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" type="primary">获取手机号并注册</u-button>
		</u--form>
	</view>
</template>

<script>
	import { regist } from '@/api/auth.js';
	export default{
		data(){
			return{
				form:{
					nickName:undefined,
					encryptedData:undefined,
					iv:undefined
				}
			}
		},
		methods:{
			getPhoneNumber(e){
				let nickName=uni.$u.trim(this.form.nickName);
				
				if(!this.form.nickName || nickName===""){
					uni.showToast({
						icon:"none",
						duration:2500,
						title:"请输入昵称"
					});
					return false;
				}
				if (e.detail.encryptedData) {
					this.form.encryptedData=e.detail.encryptedData;
					this.form.iv = e.detail.iv;
					this.registByPhone();
				}else{
				     
					 uni.showToast({
						icon:"none",
					 	title:"未获取手机号"
					 })
					 return false;
				}
			},
			registByPhone(){
				let that=this;
				uni.login({
					provider:'weixin',
					success:(res)=>{
						if(res.code){
							that.form.code=this.$u.trim(res.code);
							regist(that.form).then(res=>{
								if(res.code===200){
									uni.showToast({
										title:"注册成功"
									});
									uni.navigateBack();
								
								}
							})
						}
						else {
							console.log('登录失败！' + res.errMsg)
							uni.hideLoading();
						}
						
					}
				})
			}
		}
	}
</script>

<style>
</style>